function index_chosen = RouletteWheel(probability, num_chosen)
% Output a num_chosen*1 matrix
[num_rol num_col] = size(probability);
accumulated_probability = zeros(num_rol, 1);

index_chosen = zeros(num_chosen,1);
if(sum(probability)==0)
    for n = 1 : num_chosen
        index = fix(rand * num_rol)+1;
        index = (index>num_rol)*num_rol + (index<=num_rol)*index;
        index_chosen(n,1) = index;
    end
else
    probability = probability / sum(probability);

    for i = num_rol: -1 : 1
        accumulated_probability(i,1) = sum(probability(1:i,1));
    end

    for n = 1 : num_chosen
        rand_roulette = rand(); 
        for i = 1 : num_rol
            if(rand_roulette <= accumulated_probability(i,1))
                index_chosen(n,1) = i;
                break;
            end
        end
    end
end
